package admin

import (
	"github.com/gin-gonic/gin"
	"github.com/spf13/cast"
	"goadmin/logs"
	"goadmin/models"
)

type ProductController struct {
	BaseController
}

var ProductReq struct {
	Name        string      `json:"name" binding:"required" msg:"产品名称必填"`
	Price       interface{} `json:"price" binding:"required" msg:"产品价格必填"`
	Description string      `json:"description,omitempty"`
	Stock       interface{} `json:"stock,omitempty"`
	Status      uint8       `json:"status" binding:"required" msg:"状态必选"`
	Images      string      `json:"images" binding:"required" msg:"产品图必须上传"`
	Sales       interface{} `json:"sales"`
}

// 产品分页列表
func (con ProductController) Index(c *gin.Context) {
	productList := []models.Product{}
	res := models.DB.Find(&productList)
	con.pageSuccess(c, productList, res.RowsAffected)
}

// 创建产品
func (con ProductController) Create(c *gin.Context) {
	if err := c.ShouldBind(&ProductReq); err != nil {
		con.errorMessage(c, "参数错误: "+err.Error(), 400)
		return
	}
	price := cast.ToFloat64(ProductReq.Price)
	if price <= 0 {
		con.errorMessage(c, "产品价格必须大于0", 400)
		return
	}
	stock := cast.ToInt(ProductReq.Stock)
	sales := cast.ToInt(ProductReq.Sales)
	status := cast.ToUint8(ProductReq.Status)
	product := models.Product{
		Name:        ProductReq.Name,
		Price:       price,
		Description: ProductReq.Description,
		Stock:       stock,
		Status:      status,
		Images:      ProductReq.Images,
		Sales:       sales,
	}
	if err := models.DB.Create(&product).Error; err != nil {
		logs.Sugar().Info("创建product失败:", err)
		con.errorMessage(c, "创建失败", 500)
		return
	}
	con.successMessage(c, "添加成功", nil)
}

// 更新产品
func (con ProductController) Update(c *gin.Context) {
	id := c.Param("id")
	var product models.Product
	if err := models.DB.First(&product, id).Error; err != nil {
		con.errorMessage(c, "产品不存在", 404)
		return
	}
	if err := c.ShouldBindJSON(&ProductReq); err != nil {
		con.errorMessage(c, err.Error(), 400)
		return
	}
	price := cast.ToFloat64(ProductReq.Price)
	if price <= 0 {
		con.errorMessage(c, "产品价格必须大于0", 400)
		return
	}
	updates := map[string]interface{}{
		"Name":        ProductReq.Name,
		"Price":       price,
		"Description": ProductReq.Description,
		"Stock":       cast.ToInt(ProductReq.Stock),
		"Sales":       cast.ToInt(ProductReq.Sales),
		"Status":      cast.ToUint8(ProductReq.Status),
		"Images":      ProductReq.Images,
	}
	if err := models.DB.Model(&product).Updates(updates).Error; err != nil {
		con.errorMessage(c, "更新失败", 500)
		return
	}
	con.successMessage(c, "更新成功", nil)
}

// 删除产品
func (con ProductController) Delete(c *gin.Context) {
	id := c.Param("id")
	if err := models.DB.Delete(&models.Product{}, id).Error; err != nil {
		con.errorMessage(c, "删除失败", 500)
		return
	}
	con.successMessage(c, "删除成功", nil)
}

// 获取产品详情
func (con ProductController) Show(c *gin.Context) {
	var product models.Product
	id := c.Param("id")
	if err := models.DB.First(&product, id).Error; err != nil {
		con.errorMessage(c, "产品不存在", 500)
		return
	}
	con.successMessage(c, "获取成功", product)
}
